package dataview;

import org.jdom.Element;

public class PerStringa extends CriterioRicerca {
	private String stringa;
	private boolean appare;
	
	public PerStringa(Ordinamento ordine, String stringa, boolean appare) {
		super.ordinamento = ordine;
		this.stringa = stringa;
		this.appare = appare;
	}
	
	public Ordinamento getOrdinamento() {
		return ordinamento;
	}
	
	public String getStringa() {
		return stringa;
	}
	
	public boolean getAppare() {
		return appare;
	}
	
	public static PerStringa fromXml(Element elemento) {
		if (elemento.getName() != "per_stringa") throw new IllegalArgumentException("L'elemento passato come parametro non rappresenta una ricerca per stringa");
		return new PerStringa(Ordinamento.valueOf(elemento.getChildText("ordinamento")), elemento.getChildText("stringa"), Boolean.parseBoolean(elemento.getChildText("appare")));
	}
	
	@Override
	public Element toXml() {
		Element e = new Element("per_stringa");
		e.addContent(new Element("stringa").setText(stringa));
		e.addContent(new Element("appare").setText(Boolean.toString(appare)));
		e.addContent(new Element("ordinamento").setText(ordinamento.toString()));
		return e;
	}
	
	//PRIMA NON CONSIDERAVO L'ORDINE
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + (appare ? 1231 : 1237);
		result = prime * result + ((stringa == null) ? 0 : stringa.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (!super.equals(obj)) return false;
		if (getClass() != obj.getClass()) return false;
		PerStringa other = (PerStringa)obj;
		if (appare != other.appare) return false;
		if (stringa == null) {
			if (other.stringa != null) return false;
		} else if (!stringa.equals(other.stringa)) return false;
		return true;
	}
	
}
